/*
 * Copyright [duowan.com]
 * Web Site: http://www.duowan.com
 * Since 2005 - 2014
 */

package com.duowan.realtime.dao.impl;

import static com.duowan.common.util.ObjectUtils.isNotEmpty;

import java.util.List;

import org.springframework.dao.support.DataAccessUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;

import com.duowan.common.util.page.Page;
import com.duowan.realtime.dao.BaseSpringJdbcDao;
import com.duowan.realtime.dao.OverviewComputingGroupDao;
import com.duowan.realtime.model.OverviewComputingGroup;
import com.duowan.realtime.query.OverviewComputingGroupQuery;

/**
 * tableName: overview_computing_group
 * [OverviewComputingGroup] 的Dao操作 
 *  
 * @author badqiu email:badqiu(a)gmail.com
 * @version 1.0
 * @since 1.0
*/
public class OverviewComputingGroupDaoImpl extends BaseSpringJdbcDao implements OverviewComputingGroupDao{
	
	private RowMapper<OverviewComputingGroup> entityRowMapper = new BeanPropertyRowMapper<OverviewComputingGroup>(getEntityClass());
	
	static final private String COLUMNS = "computing_group,partition,bitset_size,element_num,create_time";
	static final private String SELECT_FROM = "select " + COLUMNS + " from overview_computing_group";
	
	@Override
	public Class<OverviewComputingGroup> getEntityClass() {
		return OverviewComputingGroup.class;
	}
	
	@Override
	public String getIdentifierPropertyName() {
		return "computingGroup";
	}
	
	public RowMapper<OverviewComputingGroup> getEntityRowMapper() {
		return entityRowMapper;
	}
	
	public void insert(OverviewComputingGroup entity) {
		String sql = "insert into overview_computing_group " 
			 + " (computing_group,partition,bitset_size,element_num,create_time) " 
			 + " values "
			 + " (:computingGroup,:partition,:bitsetSize,:elementNum,:createTime)"
			 + " ON DUPLICATE KEY UPDATE "
			 + " bitset_size=values(bitset_size), "
			 + " element_num=values(element_num) ";
		//insertWithGeneratedKey(entity,sql); //for sqlserver:identity and mysql:auto_increment
		getNamedParameterJdbcTemplate().update(sql, new BeanPropertySqlParameterSource(entity));
		
		//其它主键生成策略
		//insertWithOracleSequence(entity,"sequenceName",sql); //oracle sequence: 
		//insertWithDB2Sequence(entity,"sequenceName",sql); //db2 sequence:
		//insertWithUUID(entity,sql); //uuid
		//insertWithAssigned(entity,sql); //手工分配
	}
	
	public int update(OverviewComputingGroup entity) {
		String sql = "update overview_computing_group set "
					+ " bitset_size=:bitsetSize,element_num=:elementNum,create_time=:createTime "
					+ " where  computing_group = :computingGroup and partition = :partition ";
		return getNamedParameterJdbcTemplate().update(sql, new BeanPropertySqlParameterSource(entity));
	}
	
	public int deleteById(String computingGroup, String partition) {
		String sql = "delete from overview_computing_group where  computing_group = ? and partition = ? ";
		return  getSimpleJdbcTemplate().update(sql,  computingGroup,partition);
	}

	public OverviewComputingGroup getById(String computingGroup, String partition) {
		String sql = SELECT_FROM + " where  computing_group = ? and partition = ? ";
		return (OverviewComputingGroup)DataAccessUtils.singleResult(getSimpleJdbcTemplate().query(sql, getEntityRowMapper(),computingGroup,partition));
	}
	

	public Page<OverviewComputingGroup> findPage(OverviewComputingGroupQuery query) {
		
		StringBuilder sql = new StringBuilder("select "+ COLUMNS + " from overview_computing_group where 1=1 ");
		if(isNotEmpty(query.getComputingGroup())) {
            sql.append(" and computing_group = :computingGroup ");
        }
		if(isNotEmpty(query.getPartition())) {
            sql.append(" and partition = :partition ");
        }
		if(isNotEmpty(query.getBitsetSize())) {
            sql.append(" and bitset_size = :bitsetSize ");
        }
		if(isNotEmpty(query.getElementNum())) {
            sql.append(" and element_num = :elementNum ");
        }
		if(isNotEmpty(query.getCreateTimeBegin())) {
		    sql.append(" and create_time >= :createTimeBegin ");
		}
		if(isNotEmpty(query.getCreateTimeEnd())) {
            sql.append(" and create_time <= :createTimeEnd ");
        }
		
        //sql.append(" order by :sortColumns ");
		
		return pageQuery(sql.toString(),query,getEntityRowMapper());				
	}

	@Override
	public List<OverviewComputingGroup> getAll() {
		String sql = SELECT_FROM;
		return getJdbcTemplate().query(sql, getEntityRowMapper());
	}
}
